<template>
  <!--  委外-->
  <div class="content">
    <div class="pageSearchView">
      <el-form :inline="true" :model="searchParam" class="demo-form-inline">
        <el-form-item label="质检任务号">
          <el-input v-model="searchParam.no" placeholder="质检任务号" clearable style="width: 200px" />
        </el-form-item>
        <!-- <el-form-item label="来源单号">
          <el-input v-model="searchParam.sourceNo" placeholder="请输入来源单号" clearable style="width: 200px" />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
          <el-button class="refreshBtn" :icon="Refresh" @click="resetSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="pageTableView">
      <div class="pageTableMenuView">
        <div class="pageTableTitle">列表信息</div>
      </div>
      <!-- 表格 -->
      <el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 320px)">
        <el-table-column label="序号" align="center" width="70">
          <template #default="scope">
            <!-- 下标 + 1，计算公式：当前页数 * 每页大小 + 当前行的索引 + 1 --> {{ (pageSearch.current - 1) * pageSearch.pageSize +
              scope.$index + 1 }} </template>
        </el-table-column>
        <el-table-column prop="no" label="质检任务号" align="center" min-width="200" />
        <el-table-column prop="sourceNo" label="生产计划号" align="center" min-width="200" />
        <el-table-column prop="materialNo" label="产品编码" align="center" min-width="200" />
        <el-table-column prop="materialName" label="产品名称" align="center" min-width="200" />
        <el-table-column prop="totalQuantity" label="总体数量" align="center" min-width="200" />
        <!-- <el-table-column prop="procedureOrderNo" label="抽检数量" align="center" min-width="200" /> -->
        <el-table-column prop="passQuantity" label="合格数量" align="center" min-width="200" />
        <el-table-column prop="failQuantity" label="不合格数量" align="center" min-width="200" />
        <!-- <el-table-column prop="procedureOrderNo" label="让步接收数量" align="center" min-width="200" /> -->
        <el-table-column prop="passRate" label="合格率" align="center" min-width="200">
          <template #default="scope"> {{ scope.row.passRate || ' 0 ' }}% </template>
        </el-table-column>
        <el-table-column prop="procedureOrderNo" label="不合格率" align="center" min-width="200">
          <template #default="scope"> {{ scope.row.failRate || ' 0 ' }}% </template>
        </el-table-column>
        <!-- <el-table-column prop="procedureOrderNo" label="让步率" align="center" min-width="200" /> -->
        <!-- <el-table-column prop="procedureOrderNo" label="车间" align="center" min-width="200" />
        <el-table-column prop="procedureOrderNo" label="产线" align="center" min-width="200" />
        <el-table-column prop="procedureOrderNo" label="工位" align="center" min-width="200" /> -->
        <!-- <el-table-column prop="procedureOrderNo" label="工厂" align="center" min-width="200" /> -->
        <el-table-column prop="description" label="备注" align="center" min-width="200" />
      </el-table>
      <div class="pagePagination">
        <el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
          :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next" :total="pageTotal"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue';

onMounted(() => {
  getList();
  getOption();
});


const pageSearch = ref({
  current: 1,
  pageSize: 10
});


const workOrderList = ref({});


const pageTotal = ref(null);//页面总数
const searchParam = ref({});//页面检索条件



// 判断质检类型
const getQualityTypeByValue = (e) => {
  let name = null;
  for (const item of qualityTypeList) {
    if (e == item.value) {
      name = item.label;
    }
  }
  return name;
};

// 质检类型
const qualityTypeList = reactive([
  {
    label: '部分送货',
    value: '1',
  },
  {
    label: '全部送货',
    value: '2',
  },
  {
    label: '未送货',
    value: '0',
  }
  // 继续添加更多检查类型...
]);


const paginatedData = ref([]);
const materiaList = ref([]);//物料表
const wareList = ref([]);//仓库
const procedureOrderList = ref([]);//工序订单
const supplierList = ref([]);
const storageAllList = ref([]);
// 供应商远端搜索
const supplierSearch = async (query: string) => {
  if (query) {
    try {
      const response = await axiosInstance.post(`/api/basedataSupplier/getPage`, {
        current: 1,
        pageSize: 20,
        name: query
      });
      supplierList.value = await response.data.records; // 假设返回的数据直接是供应商列表
    } catch (error) {
      supplierList.value = []; // 出错时清空列表
    }
  }
};

const getOption = () => {
  axiosInstance.post('/api/outsourcingPlan/querySourcingPlanProcedureOrder', {})
    .then(res => {
      workOrderList.value = res.data;
    });

  // 查询所有库位
  axiosInstance.post('/api/basedataStorage/getAll', {})
    .then(res => {
      storageAllList.value = res.data;
    });


  axiosInstance.post(`/api/basedataSupplier/getPage`, {
    current: 1,
    pageSize: 20
  }).then(res => {
    if (res.code == 200) {
      supplierList.value = res.data.records;
    }
  });


  axiosInstance.post('/api/planProcedureOrder/getAll', {})
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      const list = res.data;
      const loop = [];
      for (const item of list) {
        loop.push(
          {
            value: item.id,
            label: item.materialName
          }
        );
      }
      procedureOrderList.value = list;
    });

  axiosInstance.get('/api/basedataWarehouse/treeList', {}).then(res => {
    if (res.code !== 200) {
      ElMessage.error(res.message);
      return false;
    }
    wareList.value = res.data;
  });
};

// 获取列表接口
const getList = () => {
  axiosInstance.post('/api/qualitySchemeTask/getPage', {
    ...searchParam.value,
    ...pageSearch.value,
    ...{ status: 1 }
  })
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      paginatedData.value = res.data.records;
      pageTotal.value = res.data.total;
    });
};

const handleSizeChange = (e) => {
  pageSearch.value.pageSize = e;
  getList();
};

const handleCurrentChange = (e) => {
  pageSearch.value.current = e;
  getList();
};

// 重置搜索条件
const resetSearch = () => {
  searchParam.value = {};
  pageSearch.value.current = 1;
  // current.value = 1
  getList();
};

</script>
<style scoped></style>
